//
//  WQVideoFilePath.m
//  VideoDemo
//
//  Created by C on 2019/8/19.
//  Copyright © 2019 c. All rights reserved.
//  视频导出路径

#import "WQVideoFilePath.h"

@implementation WQVideoFilePath

/// 视频地址
+ (NSString *)getVideoPathCache
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * videoCache = [[paths firstObject] stringByAppendingPathComponent:@"videos"];
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:videoCache isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) ) {
        [fileManager createDirectoryAtPath:videoCache withIntermediateDirectories:YES attributes:nil error:nil];
    };
    return videoCache;
}

/// 拼接视频文件名称
+ (NSString *)getVideoNameWithType:(NSString *)fileType
{
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HHmmss"];
    NSDate * NowDate = [NSDate dateWithTimeIntervalSince1970:now];
    NSString * timeStr = [formatter stringFromDate:NowDate];
    NSString *fileName = [NSString stringWithFormat:@"video_%@.%@",timeStr,fileType];
    return fileName;
}

/// 获取视频缓存路径
+ (NSURL *)getVideoPathURLCache
{
    NSString *rootVideoDir = [WQVideoFilePath getVideoPathCache];
    NSString *videoFileName = [WQVideoFilePath getVideoNameWithType:@"mp4"];
    NSString *videoPath = [NSString stringWithFormat:@"%@/%@",rootVideoDir,videoFileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:videoPath];
    if (existed != YES) {
        [fileManager createFileAtPath:videoPath contents:nil attributes:nil];
    }
    
    NSURL *videoUrl = [NSURL fileURLWithPath:videoPath];
    return videoUrl;
}

/// 删除路径
+ (BOOL)removeVideoPathURL:(NSURL *)url
{
    BOOL isRemove = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    if ([fileManager fileExistsAtPath:[url path]]) {
        isRemove = [fileManager removeItemAtURL:url error:&error];
    }
    return isRemove;
}

@end
